﻿using Microsoft.Graphics.Canvas.Effects;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using 纸片人.AttributeUsageFloder;
using 纸片人.InfoDataFolder;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

namespace 纸片人.GpuPage
{
    [PageType("gpu", "置换伪3D")]
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class Displacement3DPage : Page
    {
        public Displacement3DPage()
        {
            this.InitializeComponent();
            Init();
        }
        LayerBitmapData bitmapData = new LayerBitmapData();

        //是否进入来了
        bool isEntry;

        float effectWidth = 0;
        float effectHeight = 0;
        float previewWidth = 0;
        float previewHeight = 0;
        float offsetX, offsetY, scale, width, height;
        private Vector2 pos;

        private void Init()
        {
            selectPicture.Click += async (s, e) => { await bitmapData.SelectOneBitmapAsync(); canvas.Invalidate(); };
            selectPicture2.Click += async (s, e) => { await bitmapData.SelectTwoBitmapAsync(); canvas.Invalidate(); };
            canvas.CreateResources += (s, e) =>
            {
                e.TrackAsyncAction(bitmapData.LoadOneShadeCodeAsync(Shaders.ShaderType.Displacement3D).AsAsyncAction());
            };
            canvas.Draw += (s, e) =>
            {
                Function.DrawGridGraphics(e.DrawingSession);
                var element = (FrameworkElement)s;
                float effectWidth = (float)element.ActualWidth;
                float effectHeight = (float)element.ActualHeight * 0.7f;
                float previewWidth = effectWidth * 0.5f;
                float previewHeight = (float)element.ActualHeight * 0.3f;
                if (bitmapData.oneBitmap != null)
                {
                    var previewTran = Function.CalcutateImageCenteredTransform(previewWidth, previewHeight, bitmapData.oneWidth, bitmapData.oneHeight);
                    previewTran.Source = bitmapData.oneBitmap;
                    e.DrawingSession.DrawImage(previewTran, 0, effectHeight);
                }
                if (bitmapData.twoBitmap != null)
                {
                    var previewTran2 = Function.CalcutateImageCenteredTransform(previewWidth, previewHeight, bitmapData.twoWidth, bitmapData.twoHeight);
                    previewTran2.Source = bitmapData.twoBitmap;
                    e.DrawingSession.DrawImage(previewTran2, previewWidth, effectHeight);
                }
                if (bitmapData.oneBitmap != null && bitmapData.twoBitmap != null && bitmapData.oneShaderCode != null)
                {
                    var effectTran = Function.CalcutateImageCenteredTransform(effectWidth, effectHeight, bitmapData.oneWidth, bitmapData.oneHeight);

                    //是否进入范围
                    if (  (pos.X >= offsetX && pos.X <= offsetX + width && pos.Y >= offsetY && pos.Y <= offsetY + height))
                    {
                        var center = (pos - new Vector2(offsetX, offsetY)) / scale;
                        var mouse = Vector2.Normalize(center - new Vector2(bitmapData.oneWidth/2, bitmapData.oneHeight/2));
                        PixelShaderEffect effect = new PixelShaderEffect(bitmapData.oneShaderCode)
                        {
                            Source1 = bitmapData.oneBitmap,
                            Source2 = bitmapData.twoBitmap,
                            Properties = {["mouse"]=mouse}
                        };
                        effectTran.Source = effect;

                    }
                    else
                    {
                        effectTran.Source = bitmapData.oneBitmap;

                    }
                    e.DrawingSession.DrawImage(effectTran);

                }
            };


            canvas.PointerMoved += (s, e) =>
            {
                pos = e.GetCurrentPoint(canvas).Position.ToVector2();
                CalcuationPosition();
                canvas.Invalidate();
            };
        }


        void CalcuationPosition()
        {
            if (bitmapData.oneBitmap == null)
                return;


            var element = canvas;
            effectWidth = (float)element.ActualWidth;
            effectHeight = (float)element.ActualHeight * 0.7f;
            previewWidth = effectWidth * 0.5f;
            previewHeight = (float)element.ActualHeight * 0.3f;

            var mat = Function.CalcutateImageCenteredMat(effectWidth, effectHeight, bitmapData.oneWidth, bitmapData.oneHeight);
            scale = mat.M11;
            offsetX = mat.M31;
            offsetY = mat.M32;
            width = scale * bitmapData.oneWidth;
            height = scale * bitmapData.oneHeight;
        }
    }
}
